package com.app.aipass.fragment.my.reward;

import android.app.Application;

import androidx.annotation.NonNull;

import com.app.aipass.data.DemoRepository;
import com.app.aipass.entity.GetAiIndexEntity;
import com.app.aipass.entity.GetRewardEntity;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.RxUtils;

public class RewardViewModel extends BaseViewModel<DemoRepository> {

    public SingleLiveEvent<GetRewardEntity> mGetRewardEntity = new SingleLiveEvent<>();

    public RewardViewModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
    }

    public void requestNetWork() {
        model.getReward()
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<GetRewardEntity>() {

                    @Override
                    public void onNext(@NonNull GetRewardEntity getRewardEntity) {
                        if (getRewardEntity != null) {
                            mGetRewardEntity.postValue(getRewardEntity);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable throwable) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
